package org.rendao.service
{
	import com.googlecode.flexxb.FlexXBEngine;
	
	import org.rendao.entity.Question;
	import org.rendao.entity.QuestionCriteria;
	import org.rendao.entity.SearchResult;
	import org.rendao.service.base.ServiceUtils;

	public class QuestionService
	{
		private static var _instance:QuestionService;
		
		public static function get instance():QuestionService {
			if (!_instance) {
				_instance=new QuestionService(new SingletonForcer());
			}
			return _instance;
		}
		
		public function QuestionService(sf:SingletonForcer) {
		}
		
		
		private static const SEARCH_URL:String = "service/question/search";
		public function search(criteria:QuestionCriteria, callback:Function):void {
			var onSearch:Function = function(result:Object):void {
				var resultXml:XML = result as XML;
				var searchResult:SearchResult = FlexXBEngine.instance.deserialize(resultXml, SearchResult);
				callback(searchResult.records);
			};
			var content:XML = FlexXBEngine.instance.serialize(criteria);
			ServiceUtils.callService(SEARCH_URL, onSearch, content);
		}
		
		private static const SAVE_URL:String = "service/question/save";
		public function save(question:Question, callback:Function):void {
			var onSave:Function = function(data:Object):void {
				var resultXml:XML = data as XML;
				var result:Question = FlexXBEngine.instance.deserialize(resultXml, Question);
				callback(result);
			};
			var content:XML = FlexXBEngine.instance.serialize(question);
			ServiceUtils.callService(SAVE_URL, onSave, content);
		}
		
		private static const FIRST_INDEX_SUMMARY_URL:String = 
			"service/question/first_index_summary";
		public function getFirstIndexSummary(callback:Function):void {
			var onGet:Function = function(data:Object):void {
				var resultXml:XML = data as XML;
				var result:SearchResult = FlexXBEngine.instance.deserialize(resultXml, SearchResult);
				callback(result.records);
			};
			ServiceUtils.callService(FIRST_INDEX_SUMMARY_URL, onGet);
		}
	}
}
class SingletonForcer
{
}